package com.litchi.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.FindListener;

import com.lib.uil.ToastUtils;
import com.litchi.R;
import com.litchi.adapter.AddressAdapter;
import com.litchi.bean.Address;
import com.litchi.utils.Constants;
import com.litchi.utils.SwipeMenuListView.SwipeMenu;
import com.litchi.utils.SwipeMenuListView.SwipeMenuCreator;
import com.litchi.utils.SwipeMenuListView.SwipeMenuItem;
import com.litchi.utils.SwipeMenuListView.SwipeMenuListView;
import com.litchi.utils.SwipeMenuListView.SwipeMenuListView.OnMenuItemClickListener;

public class OrderAddressActivity extends Activity {

	private SwipeMenuListView mListView;
	private Context mContext;
	private List<Address> list;
	private AddressAdapter mAddressAdapter;
	private String userKey = null;
	private Button addBtn;
	private ImageView backIv;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address);
		mContext = this;
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();

	}

	public void initView() {

		userKey = getIntent().getStringExtra(Constants.USER_KEY);
		list = new ArrayList<Address>();

		backIv = (ImageView) findViewById(R.id.img_back);
		backIv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}

		});
		addBtn = (Button) findViewById(R.id.btn_add_address);
		addBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,
						AddressDetailActivity.class);
				intent.putExtra(Constants.USER_KEY, userKey);
				startActivity(intent);
			}
		});
		mListView = (SwipeMenuListView) findViewById(R.id.list_address);

		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// create "open" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(mContext);
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
						0x3F, 0x25)));
				// set item width
				deleteItem.setWidth(Constants.screenWidth / 3);
				// set item title
				deleteItem.setTitle("删除");
				// set item title fontsize
				deleteItem.setTitleSize(18);
				// set item title font color
				deleteItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(deleteItem);

			}
		};

		// set creator
		mListView.setMenuCreator(creator);

		// step 2. listener item click event
		mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				// index是menu的菜单序号
				deleteItem(position);
			}
		});

		// listview item监听事件
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent intent = new Intent();
				intent.putExtra(Constants.ADDRESS_KEY, list.get(position));
				setResult(RESULT_OK, intent);
				finish();
			}

		});

		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				list = (List<Address>) msg.obj;
				mAddressAdapter = new AddressAdapter(mContext, list);
				mListView.setAdapter(mAddressAdapter);

			}
		};

	}

	public void initData() {

		if (null != userKey) {

			BmobQuery<Address> query = new BmobQuery<Address>("Address");
			query.addWhereEqualTo("userId", userKey);
			query.findObjects(mContext, new FindListener<Address>() {

				@Override
				public void onSuccess(List<Address> arg0) {
					// list = arg0;
					Message msg = new Message();
					msg.obj = arg0;
					mHandler.sendMessage(msg);
				}

				@Override
				public void onError(int arg0, String arg1) {
					ToastUtils.showToast(mContext, arg0 + arg1);
				}
			});

		}

	}

	/**
	 * 删除列表项
	 */
	private void deleteItem(int position) {

		Address mAddressBean = list.get(position);
		list.remove(position);
		mAddressAdapter = new AddressAdapter(mContext, list);
		mListView.setAdapter(mAddressAdapter);
		mAddressAdapter.notifyDataSetChanged();

		if (null != userKey) {
			mAddressBean.setTableName("Address");
			mAddressBean.delete(mContext, new DeleteListener() {

				@Override
				public void onSuccess() {
					ToastUtils.showToast(mContext, "删除地址成功");

				}

				@Override
				public void onFailure(int arg0, String arg1) {
					 
					ToastUtils.showToast(mContext, "删除地址失败" + arg1);

				}
			});

		} else {
			ToastUtils.showToast(mContext, "请检查是否登录");
		}

	}

}
